package ldh.fx.adminfx.controller;

import javafx.animation.SequentialTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import ldh.fx.adminfx.commpont.LNavPane;
import ldh.fx.adminfx.fxanimations.FadeInRightTransition;
import ldh.fx.adminfx.fxanimations.FadeOutRightTransition;
import ldh.fx.adminfx.ui.BootstrapIndexPane;
import ldh.fx.adminfx.util.RegionUtil;
import org.kordamp.ikonli.javafx.FontIcon;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

public class AdminController implements Initializable {

    @FXML private VBox bigLeftPane;
    @FXML private VBox smallLeftPane;
    @FXML private ScrollPane leftPane;
    @FXML private StackPane contentPane;
//    @FXML private HomeHeadController headController;
    @FXML private LNavPane lNavPane;

    private Map<String, Object> contentMap = new HashMap();
    {
//        contentMap.put("index", new BootstrapIndexPane());
        contentMap.put("table", "/fxml/TablePage.fxml");
        contentMap.put("table2", "/fxml/Table2Page.fxml");
        contentMap.put("form", "/fxml/FormPane.fxml");

        contentMap.put("activity", "/fxml/ActivityPage.fxml");
        contentMap.put("customerDetail", "/fxml/CustomerDetailPage.fxml");
        contentMap.put("people", "/fxml/PeoplePage.fxml");
        contentMap.put("chartMonitor", "/fxml/ChartMonitorPage.fxml");
        contentMap.put("productList", "/fxml/ProductListPage.fxml");
    }

    private HomeHeadController headController;
    private Object currentItem = "";

    @FXML public void navTitleBtn(ActionEvent e) {
        e.consume();
    }

    @FXML public void showDashboard() {
        System.out.println("show dashboard!!!!!!!!");
    }

    @FXML public void leftNavClick(ActionEvent e) {
        Node parent = (Node) e.getSource();
        if (parent instanceof Button) {
            Object userData = ((Button) parent).getUserData();
            if (userData == null) return;
            if (userData.equals(currentItem)) return;
            currentItem = userData;
            Object obj = contentMap.get(userData.toString());
            if (obj == null) return;
            if (obj instanceof Node) {
                changeContent((Node)obj);
            } else {
                changeContent(RegionUtil.loadFxml(obj.toString()));
            }
        }
    }

    private void changeContent(Node node) {
        if (contentPane.getChildren().size() < 1) return;
        Region oldNode = (Region) contentPane.getChildren().get(0);

        FadeOutRightTransition ft = new FadeOutRightTransition(oldNode);
        ft.setOnFinished(e->{
            contentPane.getChildren().clear();
            contentPane.getChildren().add(node);
            node.setOpacity(0);
            contentPane.requestLayout();
            AnchorPane.setTopAnchor(node, 0d);
            AnchorPane.setBottomAnchor(node, 0d);
            AnchorPane.setRightAnchor(node, 0d);
            AnchorPane.setLeftAnchor(node, 0d);
        });
        FadeInRightTransition ft2 = new FadeInRightTransition((Region)node);
        SequentialTransition sequentialTransition=new SequentialTransition(ft,ft2);
        sequentialTransition.setCycleCount(1);
        sequentialTransition.play();
    }

    public void navControlHandle(ActionEvent actionEvent) {
        lNavPane.tongleLeftPane();
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
//        headController.setLNavPane(lNavPane);
    }
}
